package com.gxuwz.check.action.login;

import com.alibaba.fastjson.JSONObject;
import com.gxuwz.check.base.BaseAction;
import com.gxuwz.check.model.dto.UserDto;
import com.gxuwz.check.model.entity.SysUserEntity;
import com.gxuwz.check.service.login.ILoginService;
import com.gxuwz.check.util.JwtUtil;
import com.gxuwz.check.util.ResultJson;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class LoginAction extends BaseAction implements ModelDriven<UserDto> {
    @Autowired
    private ILoginService ILoginService;
    @Autowired
    private HttpServletRequest request;
    private UserDto user = new UserDto();
    JSONObject jsonObject = new JSONObject();
    private String userId;
    @Override
    public UserDto getModel() {
        return user;
    }

    /**
     * 登录验证以及返回token
     */
    public void login(){
        SysUserEntity sysUserEntity = ILoginService.getUserInfo(user.getUsername());
         if(sysUserEntity == null || !(sysUserEntity.getPassword()).equals(user.getPassword())){
            jsonObject.put("code",20000);
            jsonObject.put("info","isNull");
        }
        else {
            String token = JwtUtil.createJWT(700000,sysUserEntity);
            jsonObject.put("userId",sysUserEntity.getUserId()); //用于将用户表和用户对应的详细信息表的条件
            jsonObject.put("token",token);
            jsonObject.put("code",20000);
            jsonObject.put("username",user.getUsername());
        }
        ResultJson.R(jsonObject);
    }

    /**
     * 获取用户信息
     * @throws IOException
     */
    public void getUserInfo(){
        String token = ServletActionContext.getRequest().getHeader("X-Token");
        SysUserEntity userInfo = ILoginService.userInfo(userId);
        /**
         * 验证token，如果前台传来的token和后台的不符合，认证失败
         */
        if (JwtUtil.checkJWT(token,userInfo)){
            jsonObject.put("username",userInfo.getUserName());
            jsonObject.put("code",20000);
            String admin[] = {"admin"};
            String teacher[] = {"teacher"};
            String student[] = {"student"};
            if (userInfo.getRoleId() == 1){
                jsonObject.put("roles",admin);
            }
            if (userInfo.getRoleId() == 2){
                jsonObject.put("roles",teacher);
            }
            if (userInfo.getRoleId() == 3){
                jsonObject.put("roles",student);
            }
        }
        else {
            jsonObject.put("code",20000);
            jsonObject.put("info","请重新登录");
        }
        ResultJson.R(jsonObject);
    }

    /**
     * 退出登录
     */
    public void logout(){
        jsonObject.put("code",20000);
        jsonObject.put("data","success");
        ResultJson.R(jsonObject);
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}
